package com.one.rope.mvp.web.common.redis;

import com.one.rope.mvp.web.common.auth.AdminUserInfo;
import com.one.rope.mvp.web.common.auth.UserInfo;
import com.one.rope.mvp.web.config.GlobalConfig;

/**
 * @author Weijian.liu
 * @desc 登录认证redis操作
 * @date 2020/02/19
 */
public class AuthRedisOperator {

  /**
   * 保存后台管理端用户登录token
   */
  public static String saveAdminUserLoginToken(String token, AdminUserInfo adminUserInfo) throws Exception{
    String userLoginTokenKey = CommonRedisKeyGenerater.getAdminUserLoginTokenKey(token);
    return RedisClientUtil.setExObject(userLoginTokenKey, GlobalConfig.ADMIN_DEFAULT_EXPIRES_SECOND, adminUserInfo);
  }

  /**
   * 获取后台管理端用户登录token
   */
  public static AdminUserInfo getAdminUserLoginInfo(String token) throws Exception{
    String key = CommonRedisKeyGenerater.getAdminUserLoginTokenKey(token);
    return RedisClientUtil.getObject(key, AdminUserInfo.class);
  }

  /**
   * 删除后台管理端用户登录token
   */
  public static long deleteAdminUserLoginToken(String token) throws Exception{
    String key = CommonRedisKeyGenerater.getAdminUserLoginTokenKey(token);
    return RedisClientUtil.del(key);
  }

  /**
   * 保存后台管理端用户登录uerId
   */
  public static String saveAdminUserLoginUserId(String token, AdminUserInfo adminUserInfo) throws Exception{
    String userLoginTokenKey = CommonRedisKeyGenerater
        .getAdminUserLoginUserIdKey(String.valueOf(adminUserInfo.getAdminUser().getId()));
    return RedisClientUtil
        .setex(userLoginTokenKey, GlobalConfig.ADMIN_DEFAULT_EXPIRES_SECOND, token);
  }

  /**
   * 获取后台管理端用户登录uerId
   */
  public static String getAdminUserloginUserId(String userId) throws Exception{
    String key = CommonRedisKeyGenerater.getAdminUserLoginUserIdKey(userId);
    return RedisClientUtil.get(key);
  }

  /**
   * 删除后台管理端用户登录uerId
   */
  public static long deleteAdminUserLoginUserId(String userId) throws Exception{
    String key = CommonRedisKeyGenerater.getAdminUserLoginUserIdKey(userId);
    return RedisClientUtil.del(key);
  }

  /**
   * 保存小程序用户登录token
   */
  public static String saveUserLoginToken(String token, UserInfo userInfo) throws Exception{
    String userLoginTokenKey = CommonRedisKeyGenerater.getUserLoginTokenKey(token);
    return RedisClientUtil
        .setExObject(userLoginTokenKey, GlobalConfig.USER_DEFAULT_EXPIRES_SECOND, userInfo);
  }

  /**
   * 获取小程序用户登录token
   */
  public static UserInfo getUserLoginInfo(String token) throws Exception{
    String key = CommonRedisKeyGenerater.getUserLoginTokenKey(token);
    return RedisClientUtil.getObject(key, UserInfo.class);
  }

  /**
   * 删除小程序用户登录token
   */
  public static long deleteUserLoginToken(String token) throws Exception{
    String key = CommonRedisKeyGenerater.getUserLoginTokenKey(token);
    return RedisClientUtil.del(key);
  }

  /**
   * 删除小程序用户登录userId
   */
  public static long deleteUserLoginUserId(String userId) throws Exception{
    String key = CommonRedisKeyGenerater.getUserLoginUserIdKey(userId);
    return RedisClientUtil.del(key);
  }

  /**
   * 保存小程序用户登录uerId
   */
  public static String saveUserLoginUserId(String token, UserInfo userInfo) throws Exception{
    String userLoginTokenKey = CommonRedisKeyGenerater.getUserLoginUserIdKey(String.valueOf(userInfo.getMemberInfo().getId()));
    return RedisClientUtil.setex(userLoginTokenKey, GlobalConfig.USER_DEFAULT_EXPIRES_SECOND, token);
  }

  /**
   * 获取小程序用户登录uerId缓存
   */
  public static String getUserloginUserId(String userId) throws Exception{
    String key = CommonRedisKeyGenerater.getUserLoginUserIdKey(userId);
    return RedisClientUtil.get(key);
  }

  /**
   * 保存验证码缓存
   */
  public static String saveVerifyCode(String code, String mobile, Integer type) throws Exception{
    String verifyCodeKey = CommonRedisKeyGenerater.getVerifyCodeKey(mobile + ":" + type);
    return RedisClientUtil
        .setex(verifyCodeKey, GlobalConfig.VERIFY_CODE_EXPIRES_SECOND, code);
  }

  /**
   * 获取验证码缓存
   */
  public static String getVerifyCode(String mobile, Integer type) throws Exception{
    String verifyCodeKey = CommonRedisKeyGenerater.getVerifyCodeKey(mobile + ":" + type);
    return RedisClientUtil.get(verifyCodeKey);
  }
}
